<?php
namespace App\Http\Controllers\Client\Shop\Explore;

use App\Http\Controllers\ClientController;
use App\Http\Controllers\Laifei\Work\LaifeiWorkAcceptController;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use App\Models\Work;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ExploreInviteController extends ClientController
{

    public function index()
    {

        $all = $this->all;
        $work_id = $all['work_id'];

        try {
            // 工单信息
            $work_message = DB::table('explore_work')->where('work_id','=',$work_id)->first();
            // 查询订单信息
            $order_message = DB::table('explore_order')->where('eo_id','=',$work_message->eo_id)->first();
            // 查询店铺信息
            $shop_message = DB::table('shop')->where('shop_id','=',$order_message->shop_id)->first();
            // 查询地区
            $area = DB::table('dict_area')->where('id','=',$shop_message->district)->first();
            // 查询探店渠道信息
            $channel_message = DB::table('explore_channel')->where('channel_id','=',$order_message->channel_id)->first();

            $result = [
                'date'=>$order_message->start_date === $order_message->end_date? $order_message->start_date: $order_message->start_date.'~'.$order_message->end_date,
                'total_num'=>$order_message->total_num,
                'remaining_num'=>$order_message->remaining_num,
                'offline'=>$order_message->offline,
                'remark'=>$order_message->remark,
                'work_id'=>$work_message->work_id,
                'submit_time'=>$work_message->submit_time,
                'submit_img'=>AliOss::getUrl($work_message->submit_img),
                'submit_remark'=>$work_message->submit_remark,
                'hang_up'=>$work_message->hang_up,
                'accept_time'=>$work_message->accept_time,
                'accept_remark'=>$work_message->accept_remark,
                'settlement_remark'=>$work_message->settlement_remark,
                'settlement_time'=>$work_message->settlement_time,
                'status'=>$work_message->status,
                'status_name'=>Data::$exploreWorkStatus[$work_message->status],
                'error_reason'=>$work_message->error_reason?Data::$exploreWorkErrorReason[$work_message->error_reason]:null,
                'reason_detail'=>$work_message->reason_detail,
                'shop_img'=>AliOss::getUrls(explode(',', $order_message->shop_img)),
                'link_url'=>$shop_message->link_url,
                'created_at'=>$order_message->created_at,
                'shop_type'=>$shop_message->shop_type,
                'shop_type_name'=>Data::$laifeiShopTypes[$shop_message->shop_type],
                'shop_name'=>$shop_message->shop_name,
                'area'=>$area,
                'address'=>$shop_message->address,
                'platform'=>$shop_message->platform,
                'channel_id'=>$order_message->channel_id,
//                'channel_name'=>$channel_message->channel_name,
                'price'=>$channel_message->price,
                'price_amount'=>$channel_message->price/100,
                'hang_up_message'=>Work::hangUpMessage($work_id),
            ];
            return $this->setReturnJson(0,'success',$result);
        } catch (\Exception $e) {
            Log::error(ExploreInviteController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '查询失败', ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }

    }


}
